import cgi
import wsgiref.handlers
import os
import sys
import logging
import re
import string

from django.utils import simplejson
from google.appengine.api import datastore
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import search
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

# DB Entities
class Category(db.Model):
	title = db.StringProperty(required=True)

class Entry(search.SearchableModel):
	author = db.UserProperty(required=True)
	title = db.StringProperty(required=True)
	content = db.TextProperty()
	postTime = db.DateTimeProperty(auto_now_add=True)
	category = db.ReferenceProperty(Category)

class Comment(db.Model):
	author = db.UserProperty(required=True)
	content = db.TextProperty(required=True)
	postTime = db.DateTimeProperty(auto_now_add=True)
	entry = db.ReferenceProperty(Entry)

# Handlers
class FrontHome(webapp.RequestHandler):
	def get(self):
		self.response.headers['Content-Type'] = 'text/plain'
		self.response.out.write('Hello from mobiCMS!')

class Mgmt(webapp.RequestHandler):
	def updateTemplateValues(self):
		self.templateValues = {
			'page_title': 'mobiCMS - Management Home',
			'user': self.admin,
			'logout_url': users.create_logout_url('/')
		}

	def get(self):
		if not users.is_current_user_admin():
			self.redirect(users.create_login_url(self.request.uri))
		
		self.admin = users.get_current_user()
		self.updateTemplateValues()
		tmplPath = os.path.join(os.path.dirname(__file__), 'tmpl', 'mgmt', self.getTemplateName())		
		self.response.out.write(template.render(tmplPath, self.templateValues))
		
	def post(self):
		if not users.is_current_user_admin():
			self.redirect(users.create_login_url(self.request.uri))
		
		self.admin = users.get_current_user()

class MgmtHome(Mgmt):
	def updateTemplateValues(self):
		Mgmt.updateTemplateValues(self)
		self.templateValues['body_id'] = 'bdyEntries'
		
	def getTemplateName(self):
		return 'home.html'

class MgmtEntryCreate(Mgmt):
	def updateTemplateValues(self):
		Mgmt.updateTemplateValues(self)
		self.templateValues['body_id'] = 'bdyEntries'

	def getTemplateName(self):
		return 'entry_create.html'
	
	def post(self):
		Mgmt.post(self)
		entry = Entry(author=self.admin, title=self.request.get('txtTitle'))
		entry.content = self.request.get('txtContent')
		entry.save()
		self.redirect('/mgmt')

class MgmtEntryUpdate(Mgmt):
	def updateTemplateValues(self):
		Mgmt.updateTemplateValues(self)
		self.templateValues['body_id'] = 'bdyEntries'
		self.templateValues['entry_data'] = self.entry
	
	def getTemplateName(self):
		return 'entry_update.html'
	
	def get(self, entryId):
		self.entry = Entry.get_by_id(int(entryId))
		Mgmt.get(self)
		
	def post(self):
		Mgmt.post(self)
		entryId = self.request.get('hidEntryId')
		entry = Entry.get_by_id(int(entryId))
		entry.title = self.request.get('txtTitle')
		entry.content = self.request.get('txtContent')
		entry.save()
		self.redirect('/mgmt')

class MgmtEntryDelete(Mgmt):
	def getTemplateName(self):
		return 'entry_delete.html'
	
# Categories related
class MgmtCateCreate(Mgmt):
	def getTemplateName(self):
		return 'cate_edit.html'
	
	def post(self):
		pass

class MgmtCateUpdate(Mgmt):
	def getTemplateName(self):
		return 'cate_edit.html'
	
	def post(self):
		pass

class MgmtCateDelete(Mgmt):
	def getTemplateName(self):
		return 'cate_create.html'
	
	def post(self):
		pass

# URL Mapping
application = webapp.WSGIApplication(
	[
		('/', FrontHome),
		('/mgmt', MgmtHome),
		('/mgmt/cate_create', MgmtCateCreate),
		(r'/mgmt/cate_update/(\d+)', MgmtCateUpdate),
		(r'/mgmt/cate_delete/(\d+)', MgmtCateDelete)
	],
	debug = True
)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
